﻿using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;

namespace DataAccess.DAL
{
    public class UserRoleDAL : BaseDAL
    {
        public void SetUserRole(int userid, List<UserRole> userroles)
        {
            CONN_TopicSharing.Delete<UserRole>(p => p.user_id == userid);
            foreach (var item in userroles)
            {
                CONN_TopicSharing.Insert<UserRole>(item);
            }
        }

        public List<Role> GetRoles(int userid)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" select r.* from Role r ");
            sb.Append(" left join UserRole ur on ur.role_id=r.id ");
            sb.Append(" where ur.user_id=@role_id ");

            DynamicParameters dp = new DynamicParameters();
            dp.Add("@user_id", userid);

            return CONN_TopicSharing.SQLQuery<Role>(sb.ToString(), dp).ToList();
        }

        

    }
}
